package com.example.bilibili.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.bilibili.domain.BiliUser;
import com.example.bilibili.domain.BiliVideo;
import com.example.bilibili.domain.Bullet;
import com.example.bilibili.domain.Comment;
import com.example.bilibili.service.BiliUserService;
import com.example.bilibili.service.BiliVideoService;
import com.example.bilibili.service.BulletService;
import com.example.bilibili.service.CommentService;
import com.example.bilibili.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private BiliUserService biliUserService;
    @Autowired
    private BiliVideoService biliVideoService;
    @Autowired
    private BulletService bulletService;
    @Autowired
    private CommentService commentService;


    @RequestMapping("/findUserByname")
    public Object findUserByname(String username){
        QueryWrapper qw= new QueryWrapper();
        qw.select("userId","userName","userAuthor","phone","gender","birthday","userRole","fans").like("userName",username);
        qw.eq("userRole",1);
        return biliUserService.list(qw);
    }
    @RequestMapping("/findVipUserByname")
    public Object findVipUserByname(String username){
        QueryWrapper qw= new QueryWrapper();
        qw.select("userId","userName","userAuthor","phone","gender","birthday","userRole","fans").like("userName",username);
        qw.eq("userRole",2);
        return biliUserService.list(qw);
    }
    @RequestMapping("/findAllUser")
    public Object findAllUser(){
        QueryWrapper<BiliUser> qw= new QueryWrapper<>();
        qw.select("userId","userName","userAuthor","phone","gender","birthday","userRole","fans");
        qw.eq("userRole",1);
        return biliUserService.list(qw);
    }

    @RequestMapping("/findAllVipUser")
    public Object findAllVipUser(){
        QueryWrapper<BiliUser> qw= new QueryWrapper<>();
        qw.select("userId","userName","userAuthor","phone","gender","birthday","userRole","fans");
        qw.eq("userRole",2);
        return biliUserService.list(qw);
    }

   @RequestMapping("/updateUser")
    public int UpdateUser(Integer userid,String username,String phone,Integer gender){
        UpdateWrapper qw= new UpdateWrapper();
        qw.eq("userId",userid);
       BiliUser user= biliUserService.getOne(qw);
       user.setUserauthor(username);
       user.setPhone(phone);
       user.setGender(gender);
        return biliUserService.update(user,qw)?1:0;
    }

    @RequestMapping("/deleteUser")
    public int deleteUser(Integer userid){//http://localhost:8080/deleteUser?userid=22
        QueryWrapper qw0= new QueryWrapper();
        qw0.eq("userId",userid);
        BiliUser user=biliUserService.getOne(qw0);
        if(user==null)
        return 0;

        QueryWrapper<Comment> qw1= new QueryWrapper<>();
        qw1.select("id").eq("userId",userid);
        List<Comment> co=commentService.list(qw1);
        if(co!=null)
        commentService.removeByIds(co);

        QueryWrapper qw2= new QueryWrapper();
        qw2.select("id").eq("userId",userid);
        List<Bullet> bu=bulletService.list(qw2);
        if(bu!=null)
        bulletService.removeByIds(bu);

        QueryWrapper qw3= new QueryWrapper();
        qw3.select("id").eq("uploaderId",userid);
        List<BiliVideo> bv=biliVideoService.list(qw3);
        if(bv!=null)
        biliVideoService.removeByIds(bv);

        biliUserService.removeById(userid);

        return 1;
    }



}
